package jmine.tec.services.utils.replace.hssf;

import jmine.tec.services.utils.replace.api.ServiceRow;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;

import bancosys.tec.services.input.driver.xls.XLSUtils;
import bancosys.tec.services.input.exceptions.InputException;

public class HSSFServiceRow implements ServiceRow {

    private final String sheetName;

    private final HSSFServiceHeader header;

    private final HSSFRow row;

    private final XLSUtils xlsUtils;

    public HSSFServiceRow(String sheetName, HSSFServiceHeader header, HSSFRow row, XLSUtils xlsUtils) {
        this.sheetName = sheetName;
        this.header = header;
        this.row = row;
        this.xlsUtils = xlsUtils;
    }

    @Override
    public String toString() {
        return String.format("ServiceRow(%s)", this.row.getRowNum());
    }

    void addParameter(String parameter) {
        Integer index = this.header.getParameterIndex(parameter);
        if (index == null) {
            throw new RuntimeException(String.format("Parâmetro %s não encontrado.", parameter));
        }
        HSSFCell cell = this.row.getCell(index);
        while (cell != null) {
            index++;
            HSSFCell next = this.row.getCell(index);
            this.row.moveCell(cell, index.shortValue());
            cell = next;
        }
    }

    void removeParameter(String parameter) {
        Integer index = this.header.getParameterIndex(parameter);
        if (index != null) {
            HSSFCell removed = this.row.getCell(index);
            if (removed != null) {
                this.row.removeCell(removed);
            }
            for (Integer i = index + 1; i < this.header.size(); i++) {
                HSSFCell moved = this.row.getCell(i);
                if (moved != null) {
                    Integer target = i - 1;
                    this.row.moveCell(moved, target.shortValue());
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     * @see jmine.biz.invest.operacional.liquidacao.services.replace.hssf.ServiceRow#putParameter(java.lang.String, java.lang.String)
     */
    public void putParameter(String parameter, String value) {
        this.putString(this.header.getParameterIndex(parameter), value);
    }

    public void setAction(String newAction) {
        this.putString(0, newAction);
    }

    public void setName(String newName) {
        this.putString(1, newName);
    }

    /*
     * (non-Javadoc)
     * @see jmine.biz.invest.operacional.liquidacao.services.replace.hssf.ServiceRow#getStringParameter(java.lang.String)
     */
    public String getStringParameter(String parameter) {
        return this.readString(this.header.getParameterIndex(parameter));
    }

    public String getAction() {
        return this.readString(0);
    }

    public String getName() {
        return this.readString(1);
    }

    private void putString(Integer index, String value) {
        if (index == null) {
            throw new RuntimeException("Índice nulo.");
        }
        HSSFCell cell = this.row.getCell(index);
	if (cell == null){
            this.row.createCell(index);
        }
        if (value.equals("")) {
            cell.setCellValue(value);
            cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
        }
        try {
            this.xlsUtils.setCellContentFromString(this.sheetName, this.row, index, value);
        } catch (InputException e) {
            throw new RuntimeException(e);
        }
    }

    private String readString(Integer index) {
        if (index == null) {
            return null;
        }
        try {
            return this.xlsUtils.getCellContentAsString(this.sheetName, this.row, index);
        } catch (InputException e) {
            throw new RuntimeException(e);
        }
    }
}
